var express = require('express');
var router = express.Router();

const errorbook = require("../models/errorbook")
const question = require("../models/question")
const point = require("../models/point")
const menu = require("../models/menu")

const formidable = require('formidable');

router.post('/', (req, res, next) => {
    var form = new formidable.IncomingForm();
    form.parse(req, function (err, fields, files) {
        if (err) {
            res.json({ 'state': false })
        } else {
            errorbook.find({
                UUID: fields.UUID
            }, {
                attachment: 1,
                question_ID: 1, // point_id 
                _id: 0
            }, (error, id_list) => {
                if (error) {
                    res.json({ 'state': false, 'msg': error });
                } else {
                    let question_id_list = []
                    let point_id_list = []
                    for (let i = 0; i < id_list.length; i++) {
                        if (id_list[i].attachment == true) point_id_list.push(id_list[i].question_ID)
                        else if (id_list[i].attachment == false) question_id_list.push(id_list[i].question_ID)
                    }

                    var getErroredQuestion_one = () => {
                        return new Promise(function (resolve, reject) {
                            question.find({
                                _id: {
                                    $in: question_id_list
                                }
                            }, (err, list_one) => {
                                if (err) {
                                    res.json({ 'state': false, 'msg': err });
                                } else {
                                    resolve(list_one)
                                }
                            })
                        })
                    }

                    var getpoint_title = (list_one) => {
                        return new Promise(function (resolve, reject) {
                            let list = []
                            let point_id = [];
                            list_one.forEach((ele) => {
                                ele.title = ""
                                point_id.push(ele.point_id)
                            })
                            point.find({
                                _id: {
                                    $in: point_id
                                }
                            }, {
                                title: 1
                            }, (err, point_title) => {
                                if(err) {
                                    res.json({ 'state': false, 'msg': err })
                                } else {
                                    list_one.forEach((ele) => {
                                        let m = point_title.find((e) => {
                                            return e._id == ele.point_id
                                        })
                                        if( m === undefined) {
                                            list.push({
                                                question_content:  ele,
                                                question_tltle: '该知识点不存在'
                                            })
                                        } else {
                                            list.push({
                                                question_content: ele,
                                                question_tltle: m.title
                                            })
                                        }  
                                    })
                                    resolve(list)
                                }
                            })
                        })
                    }

                    var getErroredQuestion_two = (list_one) => {
                        let point_list = []
                        return new Promise(function (resolve, reject) {
                            let list = {}
                            point.find({
                                _id: {
                                    $in: point_id_list
                                }
                            }, (err, list_two) => {
                                if (err) {
                                    res.json({ 'state': false, 'msg': err });
                                } else {
                                    // console.log(list_one,'***********',list_two)
                                    // res.json({ 'state': true, 'result': {
                                    //     extraQuestion: list_one,
                                    //     attachedQuestion: list_two
                                    // }});
                                    // return
                                    list_two.forEach((ele) => {
                                        point_list.push({
                                            question_content: ele
                                        })
                                    })
                                    list.list_one = list_one
                                    list.list_two = point_list
                                    resolve(list)
                                }
                            })
                        })
                    }

                    var getmenu = (list) => {

                        menu.find({},(err, menus) => {
                            if(err) {
                                res.json({ 'state': false, 'msg': err })
                            } else {
                                list.list_one.forEach((ele) => {
                                    let menu_question = ele.question_content.position.split(",")
                                    let m = menus.find((e) => {
                                        return e.lesson === menu_question[0]
                                    })
                                    if( m === undefined) {
                                        ele.lesson = '该课程已不存在'
                                        ele.menuTitle = '该章已不存在'
                                        ele.menuSubtitle = '该节已不存在'
                                    } else {
                                        ele.lesson = m.lesson
                                        ele.menuTitle = m.menu[menu_question[1]].title
                                        ele.menuSubtitle = m.menu[menu_question[1]].subtitle[menu_question[2]]
                                    } 
                                })
                                list.list_two.forEach((ele) => {
                                    // console.log(ele)
                                    let menu_question = ele.question_content.menu.split(",")
                                    let m = menus.find((e) => {
                                        return e.lesson === menu_question[0]
                                    })
                                    ele.lesson = m.lesson
                                    ele.menuTitle = m.menu[menu_question[1]].title
                                    ele.menuSubtitle = m.menu[menu_question[1]].subtitle[menu_question[2]]
                                })
                                res.json({ 'state': true, 'result': {
                                        extraQuestion: list.list_one,
                                        attachedQuestion: list.list_two
                                    }});
                                    return
                            }
                        })
                    }

                    // getErroredQuestion_one().then(getErroredQuestion_two)
                    getErroredQuestion_one().then(getpoint_title).then(getErroredQuestion_two).then(getmenu)


                }
            })
        }

    });

});

module.exports = router;